package com;

import com.pojo.Employee;
import org.junit.Before;
import org.junit.Test;

import java.io.Reader;
import java.util.List;

public class EmployeeTest {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    @Before
    public void init() {
        String resource = "mybatis-config.xml";
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            sqlSession = sqlSessionFactory.openSession();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Test
    public void selectByPrimaryKeyTest(){
        Long employeeId = 1L;
        List<Employee> list = sqlSession.selectList("com.mapper.EmployeeMapper.selectByPrimaryKey",employeeId);
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    @Test
    public void insertTest(){
        Employee employee = new Employee();
        employee.setId(4);
        employee.setName("Tom");
        employee.setAge(25);
        employee.setPosition("员工");
        List<Employee> list = sqlSession.selectList("com.mapper.EmployeeMapper.insert",employee);

    }
    @Test
    public void updateByPrimaryKeyTest(){
        Employee employee = new Employee();
        employee.setId(4);
        employee.setName("赵六");
        employee.setAge(25);
        employee.setPosition("员工");
        List<Employee> list = sqlSession.selectList("com.mapper.EmployeeMapper.updateByPrimaryKey",employee);
    }
    @Test
    public void deleteByPrimaryKeyTest(){
        Long employeeId = 4L;
        List<Employee> list = sqlSession.selectList("com.mapper.EmployeeMapper.deleteByPrimaryKey",employeeId);
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
    @After
    public void destroy() {
        sqlSession.commit();
        sqlSession.close();
    }
}